package com.alex.projecthelper

import android.Manifest
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.MediaController
import androidx.lifecycle.lifecycleScope
import com.alex.projecthelper.databinding.ActivityMainBinding
import kotlinx.coroutines.*
import mvvm.base.helper.base.BaseVBActivity
import mvvm.base.helper.base.BaseViewModel
import mvvm.base.helper.ext.logE
import okhttp3.internal.wait
import kotlin.coroutines.resume
import kotlin.random.Random
import android.R
import android.os.Environment
import android.util.Log
import androidx.lifecycle.rxLifeScope
import com.android.ktx.coroutinescope.*
import com.hjq.permissions.XXPermissions
import java.io.*
import java.util.*


class MainActivity : BaseVBActivity<BaseViewModel, ActivityMainBinding>() {


    fun testTrue(): Boolean {
        return true
    }

    fun testFalse(): Boolean {
        return false
    }

    val uri = Uri.parse("/storage/emulated/0/aaa.avi")
    override fun initView(savedInstanceState: Bundle?) {


        lifecycleScope.launch {
            val result = Ktx.runIo {
                mutableListOf<String>()
            }.startDelay(1000)
                .repeat {

                    true
                }
                .flowOn(Dispatchers.Main)
                .distinctBy {
                }.take(10).await()
            val result1 = Ktx.runIo { "" }.asFlow()
            val result2 = Ktx.runIo { "" }.awaitResult()
            val result3 = Ktx.runIo { "" }.async(this)
        }

//        val file_path = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + "/video"
//
////        val uri = "android.resource://" + packageName + "/" + R.raw.aaa
//        val mediacontroller = MediaController(this)
//        mediacontroller.setAnchorView(mBind.videoView)
//        mBind.videoView.setMediaController(mediacontroller);
//        XXPermissions.with(this).permission(
//
//            arrayListOf(
//                Manifest.permission.READ_EXTERNAL_STORAGE,
//                Manifest.permission.WRITE_EXTERNAL_STORAGE
//            )
//        ).request { permissions, all ->
//
//            if (all) rxLifeScope.launch {
//
//                uri?.let {
//                    Ktx.runMain {
//                        mBind.videoView.setVideoURI(uri)
//                        mBind.videoView.requestFocus()
//                        mBind.videoView.start()
//                    }.await()
//                }
//            }
//
//        }
//        mBind.videoView.setOnErrorListener { mp, what, extra ->
//
//            "播放错误".logE()
//
//            true
//        }
//        mBind.videoView.setOnCompletionListener {
//            "播放完成".logE()
//            runOnUiThread {
//                mBind.videoView.setVideoURI(uri)
//                mBind.videoView.requestFocus()
//                mBind.videoView.start()
//            }
//        }


    }

    fun copyFile(name: String): Uri? {
        try {
            val dir: File = getFilesDir()
            val file = File(dir, name)
            return if (file.exists()) {
                Log.d("Test", "=========file exist=========")
                Uri.fromFile(file)
            } else {
                file.createNewFile()
                val os: OutputStream = FileOutputStream(file)
                val `is`: InputStream = assets.open(name)
                val buffer = ByteArray(1024)
                var bufferRead = 0
                while (`is`.read(buffer).also { bufferRead = it } != -1) {
                    os.write(buffer, 0, bufferRead)
                }
                os.flush()
                `is`.close()
                os.close()
                Log.d("Test", "=========copyFile success=========")
                Uri.fromFile(file)
            }
        } catch (e: FileNotFoundException) {
            e.printStackTrace()
        } catch (e: IOException) {
// TODO Auto-generated catch block
            e.printStackTrace()
        }
        return null
    }


}